package com.ztc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ztc.bean.LibraryModel;
import com.ztc.service.LibraryService;

/**
 * @类名：LibraryController.java
 * @作者：李晓平
 * @时间：2018年7月25日 下午2:03:34
 * @描述：行业库功能控制类
 */
@CrossOrigin
@RestController
public class LibraryController {
	private Logger logger = Logger.getLogger(LibraryController.class);
	
	@Autowired
	private LibraryService libraryService;
	
	/**
	 * 查询行业库列表
	 * @param libraryModel
	 * @return
	 */
	@RequestMapping("/queryLibraryList")
	public Map<String, Object> queryLibraryList(LibraryModel library) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean success = true;
		String message = "";
		try {
			List<LibraryModel> libraryList = libraryService.queryLibraryList(library);
			resultMap.put("result", libraryList);
		} catch (IllegalArgumentException ie) {
			success = false;
			message = ie.getMessage();
			logger.error(message, ie);
		} catch (Exception e) {
			success = false;
			message = "未知异常，查询行业库列表失败";
			logger.error(message, e);
		}
		resultMap.put("success", success);
		resultMap.put("message", message);
		
		return resultMap;
	}
	
	/**
	 * 查询行业库明细
	 * @param id 行业库ID
	 * @return
	 */
	@RequestMapping("/queryOneLibrary")
	public Map<String, Object> queryOneLibrary(Integer id, Integer projectId, Integer authId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean success = true;
		String message = "";
		try {
			if(id == null) {
				throw new IllegalArgumentException("请输入行业库信息ID");
			}
			if(projectId == null) {
				throw new IllegalArgumentException("请输入项目ID");
			}
			if(authId == null) {
				throw new IllegalArgumentException("请输入权限ID");
			}
			LibraryModel library = libraryService.queryOneLibrary(id, projectId, authId);
			resultMap.put("result", library);
		} catch (IllegalArgumentException ie) {
			success = false;
			message = ie.getMessage();
			logger.error(message, ie);
		} catch (Exception e) {
			success = false;
			message = "未知异常，查询行业库明细失败";
			logger.error(message, e);
		}
		resultMap.put("success", success);
		resultMap.put("message", message);
		
		return resultMap;
	}
	
	/**
	 * 添加行业库
	 * @param libraryModel
	 * @param commonLibrary 是否添加到通用行业库 0-否 1-是
	 * @return
	 */
	@RequestMapping("/addLibrary")
	public Map<String, Object> addLibrary(LibraryModel library, Integer commonLibrary) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean success = true;
		String message = "";
		try {
			if(library.getProjectId() == null) {
				throw new IllegalArgumentException("请输入项目ID");
			}
			if(StringUtils.isBlank(library.getTitle())) {
				throw new IllegalArgumentException("标题不能为空");
			}
			if(StringUtils.isBlank(library.getContent())) {
				throw new IllegalArgumentException("内容不能为空");
			}
			if(commonLibrary == null) {
				commonLibrary = 0;
			}
			libraryService.addLibrary(library, commonLibrary);
		} catch (IllegalArgumentException ie) {
			success = false;
			message = ie.getMessage();
			logger.error(message, ie);
		} catch (Exception e) {
			success = false;
			message = "未知异常，添加行业库失败";
			logger.error(message, e);
		}
		resultMap.put("success", success);
		resultMap.put("message", message);
		
		return resultMap;
	}
	
	/**
	 * 删除行业库
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteLibrary")
	public Map<String, Object> deleteLibrary(Integer id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean success = true;
		String message = "";
		try {
			if(id == null) {
				throw new IllegalArgumentException("请输入行业库信息ID");
			}
			libraryService.deleteLibrary(id);
		} catch (IllegalArgumentException ie) {
			success = false;
			message = ie.getMessage();
			logger.error(message, ie);
		} catch (Exception e) {
			success = false;
			message = "未知异常，删除行业库失败";
			logger.error(message, e);
		}
		resultMap.put("success", success);
		resultMap.put("message", message);
		
		return resultMap;
	}
}